package com.ld.businesscard_manage.service.impl;

import com.ld.businesscard_manage.DAO.RecyleDAO;
import com.ld.businesscard_manage.bean.BusinessCard;
import com.ld.businesscard_manage.bean.Recyle;
import com.ld.businesscard_manage.bean.User;
import com.ld.businesscard_manage.service.BusinessCardService;
import com.ld.businesscard_manage.service.RecyleService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecyleServiceImpl implements RecyleService {
	private BusinessCardService businessCardService = new BusinessCardServiceImpl();
	private RecyleDAO recyleDAO = new RecyleDAO();

	@Override
	public int recyleCard(Recyle recyle) {
		return recyleDAO.update("insert into recyle(`businesscard_id`,`time`,`valid`) values(?,?,?)",recyle.getBusinesscard_id(),recyle.getTime(),recyle.getValid());
	}

	@Override
	public List<Recyle> getAllRecyles() {
		return recyleDAO.queryMulti("select * from recyle",Recyle.class);
	}
	public int deleteRecyleByCardId(Integer id) {
		return recyleDAO.update("delete from recyle where `businesscard_id` = ?",id);
	}

	@Override
	public List<Recyle> getMyRecyles(String account) {
		List<BusinessCard> cards = businessCardService.getMyCards(account);
		System.out.println("cards---" + cards);
		List<Recyle> recyles = new ArrayList<Recyle>();
		for (BusinessCard card : cards) {
			if(getRecyleByCardId(card.getId()) != null)
			recyles.add(getRecyleByCardId(card.getId()));
		}
		return recyles;
	}

	@Override
	public Recyle getRecyleByCardId(Integer id) {
		return recyleDAO.querySingle("select * from recyle where businesscard_id = ?",Recyle.class,id);
	}

	@Override
	public List<Recyle> getRecylesByLike(User user, String name) {
		List<Recyle> recyles = new ArrayList<>();

		if(user.getStatus() == 1) {
			List<BusinessCard> cardsByLike = businessCardService.getCardsByLike(name);
			for (BusinessCard card : cardsByLike) {
				Recyle recyle = getRecyleByCardId(card.getId());
				if(recyle != null){
					recyles.add(recyle);
				}
			}
			return recyles;
		}else {
			List<BusinessCard> myCardsByLike = businessCardService.getMyCardsByLike(name, user.getUsername());
			for (BusinessCard card : myCardsByLike) {
				Recyle recyle = getRecyleByCardId(card.getId());
				if(recyle != null){
					recyles.add(recyle);
				}
			}
			return recyles;
		}
	}
}
